package docsync;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.TimeZone;

import docsync.doclist.DocListRequest;

public class GoogleFile implements FileInterface {

	private String export_link;
	private String edit_link; // for meta update
	private String edit_media_link; // for content update
	private String updated_time;
	private String title;
	private String last_user;
	private String last_email;
	private String etag;
	private long lastmodify;
	private long contentLength = 0;
	private DocListRequest doclistreq;

	private boolean flagnewfile = false;
	private String resourceId;
	private String contenttype;
	private ExtensionMapper extension_mapper;
	private String filename;
	private String exportformat;

	public GoogleFile(ExtensionMapper extmapper, DocListRequest dlr,
			XmlFeed feed, int entry) throws ParseException {
		this.extension_mapper = extmapper;
		this.doclistreq = dlr;
		this.resourceId = feed.resourceId(entry);
		this.export_link = feed.export(entry);
		this.edit_link = feed.edit(entry);
		this.edit_media_link = feed.editMedia(entry);
		this.updated_time = feed.updated(entry);
		this.title = feed.title(entry);
		this.last_user = feed.lastModifyUser(entry);
		this.last_email = feed.lastModifyEmail(entry);
		this.etag = feed.etag(entry);

		// this.contentLength = Long.parseLong(feed.length(entry));
		// try to guess content type
		this.contenttype = extension_mapper.fromFilename(title);

		if (this.contenttype == null) {
			this.contenttype = extension_mapper.fromResourceId(resourceId);
		}
		assert (this.contenttype != null);

		// filename
		this.filename = this.title + '.'
				+ extension_mapper.resourceIdToExtension(resourceId);
		// export format
		
		if( this.title.lastIndexOf('.')<0 ){
			this.exportformat = extension_mapper.resourceIdToExtension(resourceId);
		}else{
			this.exportformat = this.title.substring(1 + this.title.lastIndexOf('.'));
		}
		System.out.println(exportformat);
		SimpleDateFormat parse = new SimpleDateFormat(
				"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");// iso 8601
		parse.setTimeZone(TimeZone.getTimeZone("UTC"));
		this.lastmodify = parse.parse(this.updated_time).getTime();
	}

	public GoogleFile(ExtensionMapper extmapper, DocListRequest dlr,
			String filename) {
		this.extension_mapper = extmapper;
		this.doclistreq = dlr;
		this.flagnewfile = true;
		this.title = filename;
		this.contenttype = this.extension_mapper.fromFilename(filename);

	}

	public String getIdentifier() {
		return this.resourceId;
	}

	public String getFilename() {
		return this.title;
	}

	public long lastModify() {
		return this.lastmodify;
	}

	public InputStream read() throws Exception {
		return this.doclistreq.export(export_link, exportformat);
	}

	public void write(InputStream is, long length) throws Exception {
		if (this.flagnewfile) {
			// upload
			this.doclistreq.upload(title, contenttype, is, length);

		} else {
			// update
			this.doclistreq.update(this.edit_media_link, title, etag,
					contenttype, is, length);

		}
	}

	public String getEtag() {
		return this.etag;
	}

	public long length() {
		return -1;// unknown size
	}

}
